package com.hex.ds.hdtp.core.inf.data.contrast.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hex.ds.hdtp.core.inf.common.utils.JdbcConnectUtil;
import com.hex.ds.hdtp.core.inf.common.utils.JdbcUtil;
import com.hex.ds.hdtp.core.inf.metadata.po.SourceConfInfoPo;
import com.hex.ds.hdtp.core.inf.metadata.repository.ISourceConfInfoRepository;
import com.hex.ds.hdtp.core.inf.metadata.service.impl.SourceMethodHandlerContext;
import com.hex.ds.hdtp.core.inf.metadata.service.impl.TdMethodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service("COMPARE_TD")
public class TeradataInfoService extends AbstractDataInfoService {

    private final TdMethodService tdMethodService;

    public TeradataInfoService(ISourceConfInfoRepository sourceConfInfoRepository, SourceMethodHandlerContext sourceMethodHandlerContext, TdMethodService tdMethodService) {
        super(sourceConfInfoRepository, sourceMethodHandlerContext);
        this.tdMethodService = tdMethodService;
    }

    /**
     * @param sourceCode
     * @param schema
     * @param table
     * @Method selectTableExist
     * @Return java.lang.Boolean
     * @Description 是否有表
     * @Author Yin.Yang
     * @Date 2023/10/13 14:13
     * @Version V1.0
     */
    @Override
    public Boolean selectTableExist(String sourceCode, String schema, String table) throws SQLException {
        List<SourceConfInfoPo> sourceConfInfoPoList = getSourceConfInfoRepository().queryListBySourceCode(sourceCode);
        String format = tdMethodService.queryTableExistSql(schema, table);
        return JdbcConnectUtil.getSqlResult(sourceConfInfoPoList, format);
    }

    /**
     * @param sourceCode
     * @param schema
     * @param table
     * @Method selectAllFieldNum
     * @Return java.lang.Long
     * @Description 字段总数
     * @Author Yin.Yang
     * @Date 2023/10/13 14:59
     * @Version V1.0
     */
    @Override
    public Long selectAllFieldNum(String sourceCode, String schema, String table) throws SQLException {
        List<SourceConfInfoPo> sourceConfInfoPoList = getSourceConfInfoRepository().queryListBySourceCode(sourceCode);
        String format = tdMethodService.getTableAllFieldNumSql(schema, table);
        return JdbcConnectUtil.getLongResult(sourceConfInfoPoList, format);
    }

    public Map<String, String> getSourceConfInfo(String sourceCode) {
        List<SourceConfInfoPo> sourceConfInfoPoList = getSourceConfInfoRepository().queryListBySourceCode(sourceCode);
        sourceConfInfoPoList = sourceConfInfoPoList.stream().filter(sourceConf -> StrUtil.isNotBlank(sourceConf.getConfValue())).collect(Collectors.toList());
        Map<String, String> sourceConfInfo = sourceConfInfoPoList.stream().collect(Collectors.toMap(SourceConfInfoPo::getConfName, SourceConfInfoPo::getConfValue));
        sourceConfInfo.put("sourceType", sourceConfInfoPoList.get(0).getSourceType());
        String jdbcUrl = JdbcUtil.getJDBCUrl(sourceConfInfo);
        sourceConfInfo.put("url", jdbcUrl);
        return sourceConfInfo;
    }


    @Override
    protected String selectDataNumSql(String schema, String table, String whereSql) {
        return tdMethodService.getTableDataNumSql(schema, table, whereSql);
    }

    @Override
    protected String selectCountSumIsNullSql(String schema, String table, String field, String whereSql) {
        if (whereSql != null && whereSql.trim().toUpperCase().startsWith("WHERE")) {
            whereSql = "AND " + whereSql.trim().substring(5);
        }
        return tdMethodService.getTableIsNullCountSql(schema, table, field, whereSql);
    }
}
